package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.PartnerTeamAdapter;
import com.sanpu.kxe.business.main.bean.PartnerAllyOutput;
import com.sanpu.kxe.business.main.contract.PartnerTeamListContract;
import com.sanpu.kxe.business.main.presenter.PartnerTeamListPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;

/**
 * Description：合伙人团队详情
 * Created by 薛勇军 on 2018/8/6.
 */

public class PartnerTeamListActivity extends BaseTitleActivity implements PartnerTeamListContract.View {

    @Bind(R.id.list_view)
    RecyclerView vListView;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;

    private PartnerTeamAdapter mAdapter;

    private PartnerTeamListContract.Presenter mPresenter;

    private int nowPage = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_partner_team;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("我的盟友");

        vListView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PartnerTeamAdapter();
        vListView.setAdapter(mAdapter);
        mAdapter.disableLoadMoreIfNotFullPage(vListView);

        mPresenter = new PartnerTeamListPresenter(this);
        mPresenter.getPartnerAllyList(nowPage);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.getPartnerAllyList(nowPage);
                mAdapter.setEnableLoadMore(true);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getPartnerAllyList(nowPage);
            }
        }, vListView);
        mAdapter.setPreLoadNumber(2);
    }

    @Override
    public void showPartnerAllyListView(List<PartnerAllyOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size()>0) {
            if (isFirst) {
                mAdapter.setNewData(outputs);
            } else {
                mAdapter.addData(outputs);
            }
            nowPage++;
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(PartnerTeamListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
